package edu.yourl.application;

import org.apache.wicket.Request;
import org.apache.wicket.Response;
import org.apache.wicket.Session;
import org.apache.wicket.protocol.http.WebApplication;
import org.apache.wicket.request.target.coding.IndexedParamUrlCodingStrategy;
import org.apache.wicket.spring.injection.annot.SpringComponentInjector;
import org.springframework.web.context.support.WebApplicationContextUtils;

import edu.yourl.admin.web.AdminGossipPage;
import edu.yourl.admin.web.AdminPage;
import edu.yourl.domain.User;
import edu.yourl.domain.exception.NicknameAlreadyRegisteredException;
import edu.yourl.domain.service.ServiceFacade;
import edu.yourl.domain.service.ServiceLocator;
import edu.yourl.persistence.IFavoritableRepository;
import edu.yourl.persistence.IUserRepository;
import edu.yourl.web.DomainPage;
import edu.yourl.web.HomePage;
import edu.yourl.web.UrlPage;
import edu.yourl.web.UserPage;

/**
 * Application object for your web application. If you want to run this
 * application without deploying, run the Start class.
 * 
 */
public class YourlApplication extends WebApplication {
	/**
	 * Constructor
	 */
	public YourlApplication() {
	}

	@Override
	protected void init() {
		super.init();
		addComponentInstantiationListener(new SpringComponentInjector(this));
		this.mountPages();
		ServiceLocator.create(new ServiceFacade() {

			@Override
			public IFavoritableRepository getFavoritableRepository() {
				return (IFavoritableRepository) WebApplicationContextUtils
						.getRequiredWebApplicationContext(getServletContext())
						.getBean("favoritableRepository");
			}
			
			@Override
			public IUserRepository getUserRepository() {
				return (IUserRepository) WebApplicationContextUtils
				.getRequiredWebApplicationContext(getServletContext())
				.getBean("userRepository");
			}

		});
		
		createTestData();
	}

	private void createTestData() {
		IUserRepository userRepository = ServiceLocator.getInstance().getUserRepository();
		User triforce = new User("Ariel", "triforce@uol.com.ar", "123456", "triforce");
		try {
			userRepository.addUser(triforce);
		} catch(NicknameAlreadyRegisteredException e) {
			// DEmo data yacargada
			return;
		}
		
		User johnnyCash = new User("Juan Pablo", "jplagostena@hotmail.com", "123456", "JohnnyCash");
		userRepository.addUser(johnnyCash);
		
		
		
		johnnyCash.gossip("www.ole.com.ar/futbol-ascenso ta buenisimo! Todo sobre el ascenso. Miralo @triforce");
		userRepository.add(johnnyCash);
		
		johnnyCash.addFavorite(triforce);
		
		userRepository.add(johnnyCash);
		
		
		User elAutor = new User("El autor", "mail@mail.com", "123456", "eL-AuToR");
		userRepository.addUser(elAutor);
		
		User pepe = new User("Pepe", "pepe@pepe.com", "123456", "pepe");
		userRepository.addUser(pepe);
		
		triforce.gossip("Hola, visiten http://misuperpagina.com/la_pagina_de_pepe donde van a ver fotos de @pepe y @JohnnyCash. @pepe esta re loco!!. Tambien vean http://miotrapagina.com. Fin.");
		userRepository.add(triforce);
		
	}

	private void mountPages() {
		this.mountBookmarkablePage("admin", AdminPage.class);
		this.mountBookmarkablePage("gossips", AdminGossipPage.class);
		this.mount(new IndexedParamUrlCodingStrategy("url", UrlPage.class));
		this.mount(new IndexedParamUrlCodingStrategy("domain", DomainPage.class));
		this.mount(new IndexedParamUrlCodingStrategy("user", UserPage.class));
	}

	@Override
	public Session newSession(Request request, Response response) {
		return new YourlSession(request);
	}

	/**
	 * @see org.apache.wicket.Application#getHomePage()
	 */
	public Class<HomePage> getHomePage() {
		return HomePage.class;
	}
	
	

}
